package droid.frame.utils.android;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import droid.frame.App;

/**
 * {@link SharedPreferences} 工具类
 *
 * @author coffee<br>
 *         2014年5月19日上午9:14:50
 */
@SuppressLint("InlinedApi")
public class SharedPref {
    private static final String TAG = "SharedPref";

    /**
     * @param key
     * @param value
     */
    @SuppressLint("WorldWriteableFiles")
    public static void put(String key, String value) {
        // if
        // (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        // {
        // String fileName = Base64.encodeToString(key.getBytes(),
        // Base64.NO_WRAP | Base64.URL_SAFE);
        // File cacheFile = new File(Config.getSharedPrefDir(), fileName);
        // String valueEnc = Base64.encodeToString(value.getBytes(),
        // Base64.NO_WRAP | Base64.URL_SAFE);
        // FileUtils.write(cacheFile.getPath(), valueEnc);
        // Log.d(TAG, "put_sdcard " + key + "-" + value);
        // } else {
        Context context = App.getContext();
        SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
        preferences.edit().putString(key, value).commit();
        Log.d(TAG, "put -- " + key + "-" + value);
        // }
    }

    @SuppressLint("WorldWriteableFiles")
    public static String get(String key) {
        // if
        // (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        // {
        // String fileName = Base64.encodeToString(key.getBytes(),
        // Base64.NO_WRAP | Base64.URL_SAFE);
        // File cacheFile = new File(Config.getSharedPrefDir(), fileName);
        // String cacheContent = FileUtils.read(cacheFile.getPath());
        // byte[] valueDec = Base64.decode(cacheContent, Base64.NO_WRAP |
        // Base64.URL_SAFE);
        // String value = new String(valueDec);
        // Log.d(TAG, "get_sdcard " + key + "-" + value);
        // return value;
        // } else {
        Context context = App.getContext();
        return get(context.getPackageName(), key);
        // }
    }

    public static String get(String SharedPrefName, String key) {
        Context context = App.getContext();
        SharedPreferences preferences = context.getSharedPreferences(SharedPrefName, Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
        String value = preferences.getString(key, "");
        Log.d(TAG, "get -- " + key + "-" + value);
        return value;
    }

    public static void put(String key, boolean value) {
        Context context = App.getContext();
        SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        preferences.edit().putBoolean(key, value).commit();
    }
    /**
     * 获取是否是第一次访问<br>
     *
     * @return
     */
    public static boolean isFirstOpen(String key) {
        Context context = App.getContext();
        SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        // 首次访问

        boolean result = preferences.getBoolean(key, true);
        return result;
    }

    /**
     * @param open
     */
    public static void setFirstOpen(String key, boolean open) {
        put(key, open);
    }
}
